package com.system.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DataBaseControl {
    static private String hostIp;
    static private String port;
    static private String dbName;
    static private String dbUser;
    static private String password;

    public DataBaseControl(String hostIp,String port,String daName,String dbUser,String password){
        this.hostIp = "127.0.0.1";
        this.port = "3306";
        this.dbName = "marketsystem";
        this.dbUser = "root";
        this.password = "123456";
    }

    public static ResultSet execute(String sql){
        if(JdbcConnection.connection == null)
            JdbcConnection.connection = JdbcConnection.getConnection();

        Connection connection = JdbcConnection.connection;
        if(connection==null)return null;
        try {
            PreparedStatement pst = connection.prepareStatement(sql);
            String sqlType = sql.substring(0,6);
            if(sqlType.equals("select"))
            {
                return pst.executeQuery();
            }
            else
            {
                pst.execute();
                pst.close();
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
